package com.example.shop.fgo.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shop.fgo.R;
import com.example.shop.fgo.base.YBaseActivity;
import com.example.shop.fgo.base.YBaseFragment;
import com.example.shop.fgo.ui.adapter.ItemVPHomeAdapter;
import com.example.shop.fgo.ui.fragment.QTFragment;
import com.example.shop.fgo.ui.fragment.WPFragment;
import com.example.shop.fgo.ui.fragment.XCFragment;
import com.example.shop.library.util.UtilAnimatorString;

import org.xutils.common.util.DensityUtil;

public class HomeActivity extends YBaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private long mFirstBPTime = 0;
    public static final int Interval = 2000;
    private TextView mTvWpHome;
    private TextView mTvXcHome;
    private TextView mTvQtHome;
    private TextView mTvTranslateHome;
    private ViewPager mVpHome;
    private int mThreeWidth;
    private int mLastPos;



    private WPFragment mWPFragment=new WPFragment();
    private XCFragment mXCFragment=new XCFragment();
    private QTFragment mQTFragment=new QTFragment();
    private YBaseFragment[] mFragments={mWPFragment,mXCFragment,mQTFragment};
    @Override
    public boolean isNeedTitle() {
        return false;
    }

    @Override
    public int setRootView() {
        return R.layout.activity_home;
    }

    @Override
    public void initViews() {


        mTvWpHome = (TextView) findViewById(R.id.tv_wp_home);
        mTvXcHome = (TextView) findViewById(R.id.tv_xc_home);
        mTvQtHome = (TextView) findViewById(R.id.tv_qt_home);
        mTvTranslateHome = (TextView) findViewById(R.id.tv_translate_home);
        mVpHome = (ViewPager) findViewById(R.id.vp_home);
        ViewGroup.LayoutParams layoutParams = mTvTranslateHome.getLayoutParams();
        mThreeWidth = DensityUtil.getScreenWidth() / 3;
        layoutParams.width = mThreeWidth;
        mTvTranslateHome.setLayoutParams(layoutParams);
        mTvWpHome.setOnClickListener(this);
        mTvXcHome.setOnClickListener(this);
        mTvQtHome.setOnClickListener(this);
        mVpHome.setOnPageChangeListener(this);
        mTvWpHome.setTag(0);
        mTvXcHome.setTag(1);
        mTvQtHome.setTag(2);

        ItemVPHomeAdapter itemVPHomeAdapter = new ItemVPHomeAdapter(mFragmentManager, mFragments);
        //设置左右各两个
        mVpHome.setOffscreenPageLimit(2);
        mVpHome.setAdapter(itemVPHomeAdapter);


    }

    @Override
    public void initDatas() {

    }

    @Override
    public void onBackPressed() {
        /*
        * s首先执行网盘Fragment 的back，实现Fragment的back  返回true  就不执行Activity的back操作。
        * */
        if (mWPFragment.wpFragBack()) {
            Toast.makeText(mActivitySelf, "在点击一次退出", Toast.LENGTH_SHORT).show();
            return;
        }
        long timeNow = System.currentTimeMillis();
        long pastTime = timeNow - mFirstBPTime;
        if (pastTime < Interval) {
            super.onBackPressed();
        } else {
            Toast.makeText(mActivitySelf, "点的间隔太长了", Toast.LENGTH_SHORT).show();
            mFirstBPTime = timeNow;
        }

    }

    @Override
    public void onClick(View v) {
        int pos=0;
//        switch (v.getId()) {
//            case R.id.tv_wp_home:
//                pos=0;
//                break;
//            case R.id.tv_xc_home:
//                pos=1;
//                break;
//            case R.id.tv_qt_home:
//                pos=2;
//                break;
//        }
        //根据对应位置，进行移动
        doTranslate((TextView) v);
    }

    private void doTranslate(final TextView desTV) {
        final int pos= (int) desTV.getTag();
        //优选属性动画
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(mTvTranslateHome, UtilAnimatorString.translationX,mLastPos*mThreeWidth,pos*mThreeWidth);
        objectAnimator.setDuration(50);
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mLastPos=pos;
                //在动画监听内，设置textview的文字转换
                mTvTranslateHome.setText(desTV.getText());

            }
        });
        objectAnimator.start();
        mVpHome.setCurrentItem(pos);//放在这里与动画保持一致
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                doTranslate(mTvWpHome);
                break;
            case 1:
                doTranslate(mTvXcHome);
                break;
            case 2:
                doTranslate(mTvQtHome);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
